<?php
	namespace Mall\Controller;
	use Common\Controller\ApiController;
	use Util\ModelUtils;
	class VoucherController extends ApiController
	{
		/**
		 * 领取代金券
		 * @return [type] [description]
		 */
		public function getVouchers()
		{
			$input = $this->request();
			$voucher_ids = explode(",",$input['voucher_ids']);
			$model = D("UserVoucher");
			M()->startTrans();
			foreach($voucher_ids as $voucher)
			{
				$add['voucher_id'] = $voucher;
				$add['user_id'] = $this->user['id'];
				$add['create_time'] = time();
				$add['status'] = 0;
				$insert_info[]=$add;
				$result = D('Voucher')->where(['id'=>$voucher])->setDec('num',1);
			}
			$resp = $model->addAll($insert_info);
			if($resp && $result){
				M()->commit();
				$data['success']=1;
				$this->response($data);
			}else{
				M()->rollback();
				$this->errorResponse(800001,'领取失败');
			}
		}
		/**
		 * 用户代金券列表
		 * @return [type] [description]
		 */
		public function vouchersList()
		{	
			$input = $this->request();
			$page = filter($input['page'], 1);
        	$per_page = filter($input['per_page'], 10);
        	$status = filter($input['status'],0);
        	$where=[];
        	$where['user_id']=$this->user['id'];
        	// if($status != -1){
        		$where['status']=$status;
        	// }
        	$count = D('UserVoucher')->scope('valid')->where($where)->count();
			$user_vouchers = D('UserVoucher')->scope('valid')->where($where)->page($page,$per_page)->select();
			// ModelUtils::hasOne($user_vouchers,['relation'=>'Voucher','refKey'=>'id','foreignKey'=>'voucher_id','name'=>'voucher']);
			$vouchers = D('Voucher')->select();
			$vouchers = array_column($vouchers,null,'id');
			foreach($user_vouchers as &$voucher)
			{
				$cate_name = D('GoodsCate')->where(['cate_id'=>$vouchers[$voucher['voucher_id']]['cate_id']])->getField('cate_name');
				$shop_name = D('Shop')->where(['shop_id'=>$vouchers[$voucher['voucher_id']]['shop_id']])->getField('shop_name');
				$voucher['cate_name'] = $cate_name;
				$voucher['shop_name'] = $shop_name;
				$voucher['title']=$vouchers[$voucher['voucher_id']]['title'];
				$voucher['description']=$vouchers[$voucher['voucher_id']]['description'];
				$voucher['amount']=$vouchers[$voucher['voucher_id']]['amount'];
				$voucher['expire_time']=$vouchers[$voucher['voucher_id']]['expire_time'];
				$voucher['condition']=$vouchers[$voucher['voucher_id']]['condition'];
				$voucher['type']=$vouchers[$voucher['voucher_id']]['type'];
			}
			$user_vouchers = transform('UserVoucher','lists',$user_vouchers);
			$pagination = pagination($count, $per_page, $page);
			$this->response($user_vouchers,$pagination);
		}

		/**
		 * 代金券列表
		 * @return [type] [description]
		 */
		public function getVouchersList()
		{
			$input = $this->request();
			$page = filter($input['page'], 1);
        	$per_page = filter($input['per_page'], 10);
			$user_vouchers = D('UserVoucher')->where(['user_id'=>$this->user['id']])->select();
			$where=[];
			$where['expire_time']=['egt',time()];
			if($user_vouchers){
				$filter_user_voucher_ids = array_column($user_vouchers,'voucher_id');
				$where['id']=['not in',$filter_user_voucher_ids];
			}
			$count = D('Voucher')->where($where)->scope('valid')->count();
			$vouchers = D('Voucher')->scope('valid')->where($where)->page($page, $per_page)->select();
			$vouchers = transform('Voucher','details',$vouchers);
			$pagination = pagination($count, $per_page, $page);
			$this->response($vouchers,$pagination);
		}

		/**
		 * 结算页用代金券啊
		 */
		
		public function choiceVoucher()
		{
			$input = $this->request();
			$order_ids = $input['order_ids'];
			$valid_vouchers = [];
			if($order_ids){
				$orders = D('Order')->where(['order_id'=>['in',$order_ids]])->select();
						$total_price = 0;
						foreach($orders as $order){
							$total_price += $order['total_price'];
						}

						$user_vouchers = D("UserVoucher")->scope('valid')->where(['user_id'=>$this->user['id']])->select();

						if($user_vouchers){
							$user_voucher_ids = array_column($user_vouchers,'voucher_id');
							$vouchers = D("Voucher")->where(['expire_time'=>['gt',time()],'id'=>['in',$user_voucher_ids],'condition'=>['elt',$total_price]])->order('amount desc')->select();
							$filter_vouchers=array_column($vouchers,null,'id');
							foreach($user_vouchers as &$vo){
								if($filter_vouchers[$vo['voucher_id']]){
									$vo['title'] = $filter_vouchers[$vo['voucher_id']]['title'];
									$vo['description'] = $filter_vouchers[$vo['voucher_id']]['description'];
									$vo['amount'] = $filter_vouchers[$vo['voucher_id']]['amount'];
									$vo['condition'] = $filter_vouchers[$vo['voucher_id']]['condition'];
									$vo['expire_time'] = $filter_vouchers[$vo['voucher_id']]['expire_time'];
									$vo['cate_name'] = $filter_vouchers[$vo['voucher_id']]['cate_name'];
									$vo['shop_name'] = $filter_vouchers[$vo['voucher_id']]['shop_name'];
									$vo['type'] = $filter_vouchers[$vo['voucher_id']]['type'];
									if($vo['condition']<=$total_price){
										$valid_vouchers[]=$vo;
									}
								}

							}
						}
			}
			$valid_vouchers = transform('Voucher','choice',$valid_vouchers);
			$this->response($valid_vouchers);
		}

	}